package com.lcx.plugin.neteaseCloud.music.player;

import com.lcx.plugin.neteaseCloud.component.handle.SongPlayHandle;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import jmp123.PlayBack;
import jmp123.decoder.Header;
import jmp123.output.Audio;

import java.io.IOException;

/**
 * Java 播放Mp3音频，支持暂停和继续播放
 *
 * @author daxiong
 * @date 2021/10/14 下午2:32
 */
public class RealMp3Player {

    PlayBack playBack = new PlayBack(new Audio());

    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    /**
     * 初始化为未播放状态
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/14 下午2:30
     */
    public void init() {
        playBack.close();
    }

    /**
     * 判断歌词是否变化
     *
     * @return boolean
     * @author daxiong
     * @date 2021/10/28 下午2:47
     */
    public int getPlayInt() {
        Header header = playBack.getHeader();
        float t = (float) header.getFrames() * header.getFrameDuration();
        int m = (int) (t / 60.0F);
        float s = t - (float) (60 * m);
        System.out.println("m:s => " + m + ":" + s);
        int i1 = (int) ((m * 60 + s / 1) * 10);
        int i2 = (int) ((s % 1) * 10 / 1);
        return i1 + i2;
    }

    public void printProgress() {
        playBack.getHeader().printProgress();
    }

    /**
     * 暂停/继续播放
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午2:26
     */
    public void pause() {
        playBack.pause();
    }

    /**
     * 暂停/继续播放
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午2:26
     */
    public void stop() {
        playBack.stop();
    }

    /**
     * 重新播放
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午2:26
     */
    public void play() {
        init();
        new Thread(() -> {
            try {
                playBack.open(getUrl(), getUrl());
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            playBack.start(true);
            // 播放下一首歌曲
            SongPlayHandle.playNext();
        }).start();
    }

}
